package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration //表示该类是一个配置类
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)//添加拦截器
                .addPathPatterns("/**")//拦截的路径
                .excludePathPatterns("/user/login")//排除路径
                .excludePathPatterns("/user/sendMsg")//排除路径
                .excludePathPatterns("/employee/login")//排除路径
                .excludePathPatterns("/backend/**")//排除路径
                .excludePathPatterns("/front/**");//排除路径
    }

    //扩展MVC的消息转换器

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1.创建一个消息转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //TODO:最核心的操作,设置核心对象,(不设置,默认是ObjectMapper)
        messageConverter.setObjectMapper(new JacksonObjectMapper());

        //2.将消息转换器对象添加到集合中<注意:需要放在集合的最前面,优先级高一级>
        converters.add(0,messageConverter);
    }
}
